package org.burning.crm.workbench.web.controller;

import org.burning.crm.commons.constants.Constant;
import org.burning.crm.commons.domain.ReturnObject;
import org.burning.crm.workbench.domain.Bar;
import org.burning.crm.workbench.domain.Pie;
import org.burning.crm.settings.domain.User;
import org.burning.crm.workbench.domain.Customer;
import org.burning.crm.workbench.service.CustomerService;
import org.burning.crm.workbench.service.TranService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页（工作台）的控制层
 * 主要是获取用于展示的数据
 * 相关的接口往这写，前端通过ajax调用
 */
@Controller
public class WorkbenchController {
    @Autowired
    private TranService tranService;
    @Autowired
    private CustomerService customerService;
    public Object getMoneyList() {

        ReturnObject returnObject = new ReturnObject();

        return returnObject;
    }

   @RequestMapping("/workbench/main/queryCountOfTranGroupBySource.do")
    public @ResponseBody
   Object queryCountOfTranGroupBySource() {
    //查询饼图图所需的数据集合
       List<Pie> pielist = tranService.queryCountOfTranGroupBySource();
       return pielist;
    }

    @RequestMapping("/workbench/main/queryCountOfTranGroupByTotalsales.do")
    public  @ResponseBody
    Object queryCountOfTranGroupByTotalsales() {
        //查询柱状图所需的数据集合
        List<Bar> barlist = tranService.queryCountOfTranGroupByTotalsales();
        return barlist;
    }

    /**
     * 获取交易的总金额和每单平均金额
     * @return 响应信息结果
     */
    @RequestMapping("/getTranMoneyAllAndAvg")
    @ResponseBody
    public Object getTranMoneyAllAndAvg() {
        ReturnObject returnObject = new ReturnObject();
        List<Map<String, Object>> result = tranService.getTranMoneyAllAndAvg();
        if (result.size() > 0) {
            returnObject.setRetData(result);
            returnObject.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);
            returnObject.setMessage("success");
        } else {
            returnObject.setCode(Constant.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("fail");
        }
        return returnObject;
    }

    /**
     * 获取客户的总访问量
     * 在首页第一行第二个盒子展示
     * @return 响应信息结果
     */
    @RequestMapping("/getCustomerVisit")
    @ResponseBody
    public Object getCustomerVisit() {
        ReturnObject returnObject = new ReturnObject();
        List<Map<String, Object>> result = customerService.getCustomerVisit();
        System.out.println(result);
        if (result.size() > 0) {
            returnObject.setRetData(result);
            returnObject.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);
            returnObject.setMessage("success");
        } else {
            returnObject.setCode(Constant.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("fail");
        }
        return returnObject;
    }

    /**
     * 获取客户转换率
     * @return
     */
    @RequestMapping("/getCustomerCnvRate")
    @ResponseBody
    public Object getCustomerCnvRate() {
        ReturnObject returnObject = new ReturnObject();

        List<Map<String, Object>> result = customerService.getCustomerCnvRate();

        if (result.size() > 0) {
            returnObject.setRetData(result);
            returnObject.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);
            returnObject.setMessage("success");
        } else {
            returnObject.setCode(Constant.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("fail");
        }
        return returnObject;
    }

    /**
     * 所有者负责的单数、转换成交易的数量
     * @return
     */
    @RequestMapping("/getOwnerTranNum")
    @ResponseBody
    public Object getOwnerTranNum(HttpSession session) {
        ReturnObject returnObject = new ReturnObject();

        // 获取登录者
        User user = (User) session.getAttribute(Constant.SESSION_USER);

        List<Map<String, Object>> result = tranService.getOwnerTranNum();

        // 把登录者保存到结果，方便前端对比
        Map<String,Object> usermap = new HashMap<>();
        String userId = (user == null? "" :user.getId());
        usermap.put("userId",userId);
        result.add(usermap);

        if (result.size() > 0) {
            returnObject.setRetData(result);
            returnObject.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);
            returnObject.setMessage("success");
        } else {
            returnObject.setCode(Constant.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("fail");
        }
        return returnObject;
    }

    /**
     * 查询各个交易来源的数量
     * 绘制饼状图
     * @return 响应信息结果
     */
    @RequestMapping("/getTranAllSourceOfCount")
    @ResponseBody
    public Object getTranAllSourceOfCount() {
        ReturnObject returnObject = new ReturnObject();

        List<Map<String, Object>> result = tranService.getTranAllSourceOfCount();

        if (result.size() > 0) {
            returnObject.setRetData(result);
            returnObject.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);
            returnObject.setMessage("success");
        } else {
            returnObject.setCode(Constant.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("fail");
        }
        return returnObject;
    }

}
